package com.nf.health.app.intervention_details.adapter;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.nf.health.app.R;
import com.nf.health.app.intervention_details.activity.HealthSurveyActivity;
import com.nf.health.app.intervention_details.bean.QuestionListBean;
import com.nf.health.app.utils.ActivityUtils;
import com.nf.health.app.utils.DensityUtil;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by 恩福 on 2016/11/17.
 * 个人健康信息汇总 Adapter
 *
 * @auther changpeng
 * @email obj_cp@126.com
 */

public class FollowupAdapter extends RecyclerView.Adapter {
    private static final int TYPE_HEAD = 0;
    private static final int TYPE_CONTENT = 1;
    private String mHospital;

    private Context mContext;
    List<QuestionListBean> mQuestionListBeans;

    public FollowupAdapter(Context context, List<QuestionListBean> questionListBeans, String hospital) {
        this.mContext = context;
        this.mQuestionListBeans = questionListBeans;
        this.mHospital = hospital;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        if (TYPE_HEAD == viewType) {
            TextView textView = new TextView(mContext);
            int padding = DensityUtil.dip2px(mContext, 10);
            textView.setPadding(padding, padding, padding, padding);
            textView.setText("当前为"+mHospital+"健康管理方案，以下是您的干预调查问卷");
            return new HeadHolder(textView);
        }

        if (TYPE_CONTENT == viewType) {
            view = LayoutInflater.from(mContext).inflate(R.layout.item_followup, parent, false);
            return new FollowupHolder(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof FollowupHolder) {
            final QuestionListBean questionListBean = mQuestionListBeans.get(position - 1);
            FollowupHolder followupHolder = (FollowupHolder) holder;
            if (position == 1) {
                followupHolder.vTopline.setVisibility(View.INVISIBLE);
                followupHolder.vBottomline.setVisibility(View.VISIBLE);
            }
            if (position == mQuestionListBeans.size()) {
                followupHolder.vTopline.setVisibility(View.VISIBLE);
                followupHolder.vBottomline.setVisibility(View.INVISIBLE);
            }

            if (mQuestionListBeans.size() == 1){
                followupHolder.vTopline.setVisibility(View.INVISIBLE);
                followupHolder.vBottomline.setVisibility(View.INVISIBLE);
            }
            followupHolder.tvTime.setText(questionListBean.getSurveytime());
            followupHolder.tvCount.setText("第"+(position)+"次干预调查问卷");

            followupHolder.tvWrite.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Bundle bundle = new Bundle();
                    bundle.putString(HealthSurveyActivity.EXTRA_MSG_ID, questionListBean.getId());
                    ActivityUtils.showActivity(mContext, HealthSurveyActivity.class, bundle);
                }
            });
            String quesTime = questionListBean.getSurveytime();

            SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-MM-dd");
            Date curDate = new Date(System.currentTimeMillis());//获取当前时间
            String time = formatter.format(curDate);
            try {
                Date quesTimeDate = formatter.parse(quesTime);
                long quesTimeLong = quesTimeDate.getTime()/1000; //问卷时间

                Date curTimeDate = formatter.parse(time);
                long curTimeLong = curTimeDate.getTime()/1000; //当前时间

                if(quesTimeLong <= curTimeLong){  //问题时间 < 当前时间 ，也就是在现在之前 推的问卷 允许填写
                    followupHolder.tvWrite.setEnabled(true);
                    followupHolder.tvWrite.setTextColor(mContext.getResources().getColor(R.color.theme_color));
                }else{
                    //还没到时间，不让填写
                    followupHolder.tvWrite.setEnabled(false);
                    followupHolder.tvWrite.setTextColor(mContext.getResources().getColor(R.color.black8));
                }

                if(questionListBean.getState().equals("1")){
                    followupHolder.tvWrite.setEnabled(false);
                    followupHolder.tvWrite.setBackgroundResource(0);
                    followupHolder.tvWrite.setTextColor(mContext.getResources().getColor(R.color.theme_color));
                    followupHolder.tvWrite.setText("已完成");
                }

                if(time.equals(quesTime)){
                    followupHolder.ivTimeMark.setSelected(true);
                }else{
                    followupHolder.ivTimeMark.setSelected(false);
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public int getItemCount() {
        return mQuestionListBeans == null ? 0 : mQuestionListBeans.size() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        switch (position) {
            case 0:
                return TYPE_HEAD;
        }
        return TYPE_CONTENT;
    }

    class FollowupHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.tv_time)
        TextView tvTime;
        @Bind(R.id.v_topline)
        View vTopline;
        @Bind(R.id.iv_time_mark)
        ImageView ivTimeMark;
        @Bind(R.id.v_bottomline)
        View vBottomline;
        @Bind(R.id.tv_count)
        TextView tvCount;
        @Bind(R.id.tv_write)
        TextView tvWrite;

        public FollowupHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    class HeadHolder extends RecyclerView.ViewHolder {

        public HeadHolder(View itemView) {
            super(itemView);
        }
    }
}
